<script setup>
import {
    Edit, Delete, Position, Upload
} from '@element-plus/icons-vue'

import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
    addpathservice, getallbookservice, getpartpathservice,
    updatepathservice, detailbookservice, deletepathservice, lendbookservice, getallbookcategoryservice
} from '@/api/book.js'

import {
    getpaths, getpersons, addcheckdayservice, getallpathservice, AIadddaycheckservice, AIaddregularchecktaskservice,
    addcheckregularservice, getparttaskservice, updatetaskservice, deletetaskservice
} from '@/api/allApi.js'
//控制添加分类弹窗
const dialogVisible = ref(false)
const lenddialogVisible = ref(false)
//分页条数据模型
//const pageNum = ref(1)//当前页
const total = ref(20)//总条数
//const pageSize = ref(3)//每页条数
//文章分类数据模型
const categorys = ref([
])
//用户搜索时选中的分类id
const categoryId = ref('')
const bookcategorylist = async () => {
    let result = await getallbookcategoryservice();
    categorys.value = result.data;
}
bookcategorylist();
//用户搜索时选中的发布状态
const state = ref('')
//定义变量，控制标题
const title = ref('')
//交通路线列表数据模型
const tasks = ref([])
const paths = ref([])
const allpath = ref([])
const allperson = ref([])
const list = async () => {
    let result = await getpaths();
    allpath.value = result.data;
    result = await getpersons();
    allperson.value = result.data;
}
list();
//与后端交互的json数据

const taskmodel = ref({
    pathName: null,
    userName: null,
    taskTime: null
})
const findmodel = ref({
    pageNum: 1,
    pageSize: 20,
    str: ''
})

const tasklist = async () => {
    console.log(findmodel.value);
    let result = await getparttaskservice(findmodel.value);
    console.log(result);
    total.value = result.data.total;
    tasks.value = result.data.items;
    console.log(tasks.value);
}
tasklist();
const pathlist = async () => {
    let result = await getallpathservice();
    paths.value = result.data;
    console.log(paths.value);
}
pathlist()


//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
    findmodel.value.pageSize = size;
    tasklist();
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
    findmodel.value.pageNum = num;
    tasklist();
}
const cleardata = () => {
    findmodel.value.startPoint = '',
        findmodel.value.endPoint = '',
        findmodel.value.str = ''
}
//表单校验
const rules = {
    pathName: [
        { required: true, message: '请选择城镇道路', trigger: 'blur' },
    ],
    userName: [
        { required: true, message: '请选择检查人员', trigger: 'blur' },
    ],
    taskTime: [
        { required: true, message: '请选择检查日期', trigger: 'blur' },
    ],
}
const adddaycheck = () => {
    taskmodel.value.pathName = ''
    taskmodel.value.userName = ''
    taskmodel.value.taskTime = ''
    dialogVisible.value = true;
    title.value = '添加日常巡检'
}
const addregularcheck = () => {
    taskmodel.value.pathName = ''
    taskmodel.value.userName = ''
    taskmodel.value.taskTime = ''
    dialogVisible.value = true;
    title.value = '添加定期检查'
}


const addcheckday = async () => {
    console.log(taskmodel.value);
    let result = await addcheckdayservice(taskmodel.value);
    if (result.code == 0) {
        ElMessage.success("添加成功")
        dialogVisible.value = false;
        tasklist();
    }
    else {
        ElMessage.error(result.message ? result.message : '添加失败')
    }
}
const addcheckregular = async () => {
    console.log(taskmodel.value);
    let result = await addcheckregularservice(taskmodel.value);
    if (result.code == 0) {
        ElMessage.success("添加成功")
        dialogVisible.value = false;
        tasklist();
    }
    else {
        ElMessage.error(result.message ? result.message : '添加失败')
    }
}
const updatetask = async () => {
    console.log(taskmodel.value);
    let result = await updatetaskservice(taskmodel.value);
    if (result.code == 0) {
        ElMessage.success("修改成功")
        dialogVisible.value = false;
        tasklist();
    }
    else {
        ElMessage.error(result.message ? result.message : '修改失败')
    }
}
//删除分类
const del = (row) => {
    ElMessageBox.confirm(
        '你确认删除该条任务信息吗？',
        '温馨提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(async () => {
            //用户点击了确认
            let result = await deletetaskservice(row.id)

            //再次调用getAllCategory，获取所有文章分类
            tasklist();
            if (result.code === 0) {
                ElMessage.success('删除成功')
                dialogVisible.value = false;
                tasklist();
            }
            else {
                ElMessage.error(result.message ? result.message : '修改失败')
            }
        })
        .catch(() => {
            //用户点击了取消
            ElMessage({
                type: 'info',
                message: '取消删除',
            })
        })
}
//展示编辑弹窗
const showDialog = (row) => {
    title.value = '编辑人员安排';
    dialogVisible.value = true;
    //数据拷贝
    taskmodel.value.pathName = row.pathName
    taskmodel.value.userName = row.userName
    taskmodel.value.taskTime = row.taskTime
    //扩展i的属性，将来需要传递给后台，完成分类的修改
    taskmodel.value.id = row.id;
    console.log(row.id);
}
const decideways = () => {
    if (title.value === '添加日常巡检') addcheckday()
    else if (title.value === '添加定期检查') addcheckregular()
    else if (title.value === '编辑人员安排') updatetask()
}
const AIadddaycheck = async () => {
    let result = await AIadddaycheckservice(taskmodel.value);
}
const AIaddregularchecktask = async () => {
    let result = await AIaddregularchecktaskservice(taskmodel.value);
}
setInterval(function () {
    for (let i = 0; i < paths.value.length; i++) {
        console.log(paths.value[i]);
        if (paths.value[i].level1 === 'I等养护') {
            console.log(paths.value[i].pathName);
            taskmodel.value.pathName = paths.value[i].pathName
            taskmodel.value.userName = ''
            taskmodel.value.taskTime = ''
            AIadddaycheck();
            tasklist();
        }
    }
}, 24 * 60 * 60 * 1000); //日常检查I等养护定时器 每隔一天 生成任务

setInterval(function () {
    for (let i = 0; i < paths.value.length; i++) {
        console.log(paths.value[i]);
        if (paths.value[i].level1 === 'II等养护') {
            console.log(paths.value[i].pathName);
            taskmodel.value.pathName = paths.value[i].pathName
            taskmodel.value.userName = ''
            taskmodel.value.taskTime = ''
            AIadddaycheck();
            tasklist();
        }
    }
}, 2 * 24 * 60 * 60 * 1000);//日常检查II等养护定时器 每隔两天 生成任务

setInterval(function () {
    for (let i = 0; i < paths.value.length; i++) {
        console.log(paths.value[i]);
        if (paths.value[i].level1 === 'III等养护') {
            console.log(paths.value[i].pathName);
            taskmodel.value.pathName = paths.value[i].pathName
            taskmodel.value.userName = ''
            taskmodel.value.taskTime = ''
            AIadddaycheck();
            tasklist();
        }
    }
}, 3 * 24 * 60 * 60 * 1000);//日常检查III等养护定时器 每隔三天 生成任务

setInterval(function () {
    for (let i = 0; i < paths.value.length; i++) {
        console.log(paths.value[i]);
        console.log(paths.value[i].pathName);
        taskmodel.value.pathName = paths.value[i].pathName
        taskmodel.value.userName = ''
        taskmodel.value.taskTime = ''
        AIaddregularchecktask();
        tasklist();

    }
}, 365 * 24 * 60 * 60 * 1000);//定期检查

//24*60*60*1000
</script>

<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>日常巡检和定期检查任务安排</span>
                <div class="extra">
                    <el-button type="primary" @click="adddaycheck">添加日常巡检</el-button>
                    <el-button type="primary" @click="addregularcheck">添加定期检查</el-button>
<!--                  测试使用-->
                </div>
            </div>
        </template>
        <!-- 搜索表单 -->
        <el-form inline>
            <el-form-item label="查询条件：">
                <el-input v-model="findmodel.str" placeholder="请输入查询内容" width="200"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="tasklist">搜索</el-button>
                <el-button @click="cleardata">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 文章列表 -->
        <el-table :data="tasks" style="width: 100%">
            <el-table-column label="任务类型" prop="taskType" width="100"></el-table-column>
            <el-table-column label="任务名称" prop="taskName"></el-table-column>
            <el-table-column label="用户名" prop="userName"></el-table-column>
            <el-table-column label="用户类型" prop="userType"></el-table-column>
            <el-table-column label="道路名称" prop="pathName"></el-table-column>
            <el-table-column label="路面类型" prop="pavementType"></el-table-column>
            <!-- <el-table-column label="起始地址" prop="startAddress" ></el-table-column>
            <el-table-column label="结束地址" prop="endAddress" > </el-table-column> -->
            <el-table-column label="巡检日期" prop="taskTime" width="180"> </el-table-column>
            <el-table-column label="状态" prop="status"> </el-table-column>
            <el-table-column label="操作" width="100">
                <template #default="{ row }">
                    <!-- <el-button :icon="Position" circle plain @click="arrangetask(row)"></el-button> -->
                    <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="del(row)"></el-button>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>
        <!-- 分页条 -->
        <el-pagination v-model:current-page="findmodel.pageNum" v-model:page-size="findmodel.pageSize"
            :page-sizes="[20, 30, 40, 50]" layout="jumper, total, sizes, prev, pager, next" background :total="total"
            @size-change="onSizeChange" @current-change="onCurrentChange"
            style="margin-top: 20px; justify-content: flex-end" />
    </el-card>
    <!-- 添加分类弹窗 -->
    <el-dialog v-model="dialogVisible" :title="title" width="30%">
        <el-form :model="taskmodel" :rules="rules" label-width="100px" style="padding-right: 30px">
            <el-form-item label="城镇道路" prop="pathName">
                <el-select placeholder="请选择" v-model="taskmodel.pathName">
                    <el-option v-for="c in allpath" :key="c" :label="c" :value="c">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="检查人员" prop="userName">
                <el-select placeholder="请选择" v-model="taskmodel.userName">
                    <el-option v-for="c in allperson" :key="c" :label="c" :value="c">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="日期" prop="taskTime">
                <el-date-picker v-model="taskmodel.taskTime" type="datetime" placeholder="选择日期时间">
                </el-date-picker>
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="decideways"> 确认 </el-button>
            </span>
        </template>
    </el-dialog>
</template>


<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

}

.extra {
    display: flex;
    justify-content: flex-end;
}
</style>